﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReadR41;
using Dimeng.ArdisPostProcessor.HomagLC4.Ardis;

namespace Dimeng.ArdisPostProcessor.HomagLC4.Ardis
{
    public class R41Data
    {
        public string R41Name { get; set; }
        public List<Part> Parts = new List<Part>();
        public ParameterList ParameterList = new ParameterList();
        public List<Material> Materials = new List<Material>();
        public List<Pattern> Patterns = new List<Pattern>();
        public List<Sheet> Sheets = new List<Sheet>();
        public List<Cut> Cuts = new List<Cut>();
        public List<ExternCut> FullCuts = new List<ExternCut>();
        public List<Cut> BlockCuts = new List<Cut>();
        public List<ExternPart> PatternParts = new List<ExternPart>();
        /// <summary>
        /// 保存解析R41文件后的数据
        /// </summary>
        public R41Data(string r41name)
        {
            R41Name = r41name;
        }

        public override string ToString()
        {
            return R41Name;
        }

        /// <summary>
        /// 分析数据，得到每个大板的对应的板件清单
        /// </summary>

        public void Analysis()
        {
            List<int> PrintedIDs = new List<int>();
            foreach (Pattern pattern in Patterns)
            {
                var cuts = Cuts.Where(c => c.CutPatternNo == pattern.PatternNumber && c.CutPartNo != 0)
                               .ToList();

                foreach (var cut in cuts)
                {
                    //找到cut对应的part记录
                    Part p = Parts.Single(d => d.PartNo == cut.CutPartNo);
                    int parentcutqty = Cuts.Find(it => it.CutNo == cut.CutParentNo).CutQty;
                    int totalqty = cut.CutQty * pattern.PatternQty * parentcutqty;
                    int counter = 0;//计数器
                    for (int o = 0; o < totalqty; o++)//执行多次，直到全部找到
                    {
                        if (PrintedIDs.Count(i => i == p.PartNo) < p.PartQty)
                        {
                            if (counter == totalqty) break;//到了这个数就不继续了
                            PrintedIDs.Add(p.PartNo);
                            ExternPart CopyPart = new ExternPart()
                            {
                                PartNo = p.PartNo,
                                PartQty = 1,
                                PartRef = p.PartRef,
                                PartD = p.PartD,
                                PartDrawing = p.PartDrawing,
                                PartL = p.PartL,
                                PartW = p.PartW,
                                PartRemark = p.PartRemark,
                                PartRemark2 = p.PartRemark2,
                                PartRemark3 = p.PartRemark3,
                                PartCalc = p.PartCalc,
                                PatternId = pattern.PatternNumber
                            };
                            PatternParts.Add(CopyPart);
                            counter++;
                        }
                        else
                        {
                            //去找那些id不同，但尺寸、材料相同的家伙们
                            List<Part> parts = Parts.Where(tempp =>
                                                               tempp.PartNo != p.PartNo
                                                               &&
                                                               tempp.PartL == p.PartL
                                                               &&
                                                               tempp.PartW == p.PartW
                                                               &&
                                                               tempp.PartMatNo == p.PartMatNo
                                                               &&
                                                               tempp.PartD == p.PartD
                                                               ).ToList();
                            foreach (Part tp in parts)
                            {
                                if (counter == totalqty) break;//到了这个数就不继续了

                                if (PrintedIDs.Count(a => a == tp.PartNo) < tp.PartQty)
                                {
                                    PatternParts.Add(new ExternPart()
                                    {
                                        PartNo = tp.PartNo,
                                        PartQty = 1,
                                        PartRef = tp.PartRef,
                                        PartD = tp.PartD,
                                        PartDrawing = tp.PartDrawing,
                                        PartL = tp.PartL,
                                        PartW = p.PartW,
                                        PartRemark = tp.PartRemark,
                                        PartRemark2 = tp.PartRemark2,
                                        PartRemark3 = tp.PartRemark3,
                                        PartCalc = tp.PartCalc,
                                        PatternId = pattern.PatternNumber
                                    }
                                                                );
                                    PrintedIDs.Add(tp.PartNo);
                                    counter++;
                                }
                            }
                        }
                    }
                }
            }
        }

    }
}
